package ofpof.service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

@Service
public class CookieHelper {
	
	public Cookie getCookie(HttpServletRequest request,String cookieName) {
		Cookie foundCookie = null;
		Cookie[] cookies = request.getCookies();
		if ((cookies != null) && (cookies.length > 0)) {
			for (Cookie cookie : cookies) {
				if (cookieName.equalsIgnoreCase(cookie.getName())) {
					foundCookie = cookie;
					break;
				}
			}
		}
		return foundCookie;
	}
	
	public void removeCookie(HttpServletResponse response, Cookie cookie) {
		cookie.setMaxAge(0);
		cookie.setPath("/");
		response.addCookie(cookie);
	}
	
	public void createSessionCookie(HttpServletResponse response,String name,String value) {
		Cookie cookie = new Cookie(name, value);
		cookie.setPath("/");
		response.addCookie(cookie);

	}
	
	public void createPersistentCookie(HttpServletResponse response,String name,String value,int expiry) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(expiry);
		cookie.setPath("/");
		response.addCookie(cookie);
	}

}
